package com.franklin.ideaplugin.mybatisrunner.xml.params;

import com.alibaba.fastjson2.annotation.JSONField;
import com.franklin.ideaplugin.mybatisrunner.event.events.AbstractSqlEvent;
import com.franklin.ideaplugin.mybatisrunner.event.events.AddTestEvent;
import com.franklin.ideaplugin.mybatisrunner.event.events.RemoveTestEvent;
import com.intellij.psi.PsiElement;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.util.ui.FormBuilder;
import lombok.NoArgsConstructor;

import javax.swing.*;

/**
 * @author Ye Junhui
 * @since 2024/2/20
 */
public class PredicateParam extends MybatisParam {

    private JBCheckBox checkBox = new JBCheckBox();

    /**
     * 是否勾选
     */
    private boolean checked;

    public PredicateParam(PsiElement psiElement) {
        super(psiElement);
    }

    @Override
    public JComponent buildComponent(Runnable action) {
        checkBox.setText(elExpression);
        checkBox.addActionListener(e -> {
            action.run();
        });

        FormBuilder formBuilder = FormBuilder.createFormBuilder();
        formBuilder.addLabeledComponent("if", checkBox);

        return formBuilder.getPanel();
    }

    @Override
    public AbstractSqlEvent buildEvent() {
        checked = checkBox.isSelected();
        if (checked) {
            return new AddTestEvent(elExpression);
        }
        return new RemoveTestEvent(elExpression);
    }

    @Override
    public void load(String cache) {
        checked = true;
        checkBox.setSelected(true);
    }
}
